package com.first.nell.me

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.first.nell.R
import com.first.nell.base.BaseActivity
import com.first.nell.data.ReqSaveFeedback
import com.first.nell.databinding.ActivityFirstNellFeedbackBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.getFirstNellSafeFirstNellTag
import com.first.nell.extend.recordFirstNellClickFirstNellEvent

@SuppressLint("CustomSplashScreen")
class FeedbackActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellFeedbackBinding
    private lateinit var mFeedbackTypeAdapter: FeedbackTypeAdapter

    private val mMeViewModel by lazy {
        ViewModelProvider(this).get(MeViewModel::class.java)
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mMeViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.feedback.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding = ActivityFirstNellFeedbackBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //初始化控件
            mFeedbackTypeAdapter =
                FeedbackTypeAdapter(mMeViewModel.mFirstNellFeedbackFirstNellTypeFirstNellList)
            mFeedbackTypeAdapter.setFirstNellOnFirstNellItemFirstNellClickFirstNellListener { index, cashVistaPopListItem ->
                mMeViewModel.mFirstNellFeedbackFirstNellTypeFirstNellList.forEachIndexed { position, item ->
                    item.isChecked = index == position
                }
                mFeedbackTypeAdapter.notifyDataSetChanged()
            }

            //保存反馈回调
            mMeViewModel.onFirstNellSaveFirstNellFeedbackFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    mFirstNellActivityFirstNellBinding.inputFeedbackContent.setFirstNellContent("")
                    Toast.makeText(
                        this,
                        getString(R.string.first_nell_success_flag),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            //点击保存
            mFirstNellActivityFirstNellBinding.submitFeedback.setOnClickListener {
                val formDataCompleteState = isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted()
                mFirstNellActivityFirstNellBinding.submitFeedback.recordFirstNellClickFirstNellEvent(
                    formDataCompleteState
                )
                if (formDataCompleteState.isBlank()) {
                    var type =
                        mMeViewModel.mFirstNellFeedbackFirstNellTypeFirstNellList.first().content
                    mMeViewModel.mFirstNellFeedbackFirstNellTypeFirstNellList.forEach { item ->
                        if (item.isChecked) {
                            type = item.content
                        }
                    }
                    val content = mFirstNellActivityFirstNellBinding.inputFeedbackContent.getFirstNellContent()
                    mMeViewModel.saveFirstNellFeedback(
                        ReqSaveFeedback(
                            type = type,
                            opinion = content
                        )
                    )
                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.first_nell_please_complete_all_required_fields),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            //初始化反馈类型
            mMeViewModel.initFirstNellFeedbackFirstNellType()

            //初始化列表
            mFirstNellActivityFirstNellBinding.rcFeedbackTypeList.adapter = mFeedbackTypeAdapter
            mFirstNellActivityFirstNellBinding.rcFeedbackTypeList.layoutManager = GridLayoutManager(this, 2)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        val selectType = mMeViewModel.mFirstNellFeedbackFirstNellTypeFirstNellList.any {
            it.isChecked
        }
        if (!selectType) {
            notFillOptions.add("FEEDBACK_TYPE")
        }

        val content = mFirstNellActivityFirstNellBinding.inputFeedbackContent.getFirstNellContent()
        if (content.isBlank()) {
            val gTag = mFirstNellActivityFirstNellBinding.inputFeedbackContent.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }
}